package com.pfzy.utildemo.activity.record.soundrecord.util;

import android.os.Environment;
import android.os.StatFs;

import java.io.File;
import java.io.FileInputStream;
import java.text.NumberFormat;
import java.util.Locale;

public class FileSizeUtil {
    public static final int SIZE_TYPE_B = 1;
    public static final int SIZE_TYPE_KB = 2;
    public static final int SIZE_TYPE_MB = 3;
    public static final int SIZE_TYPE_GB = 4;

    public static double getFileOrFilesSize(String filePath, int sizeType) {
        File file = new File(filePath);
        long blockSize = 0;
        try {
            if (file.isDirectory()) {
                blockSize = getFileDircetorySizes(file);
            } else {
                blockSize = getFileSize(file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return formatFileSize(blockSize, sizeType);
    }

    private static long getFileSize(File file) throws Exception {
        FileInputStream fis;
        long size = 0;
        if (file.exists()) {
            try {
                fis = new FileInputStream(file);
                size = fis.available();
                if (file != null) {
                    fis.close();
                }
            } catch (Throwable th) {
                throw th;
            }
        } else {
            file.createNewFile();
        }
        return size;
    }

    private static long getFileDircetorySizes(File f) throws Exception {
        long fileSize;
        long size = 0;
        File[] flist = f.listFiles();
        for (int i = 0; i < flist.length; i++) {
            if (flist[i].isDirectory()) {
                fileSize = getFileDircetorySizes(flist[i]);
            } else {
                fileSize = getFileSize(flist[i]);
            }
            size += fileSize;
        }
        return size;
    }

    private static double formatFileSize(long fileS, int sizeType) {
        NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.getDefault());
        numberFormat.setMaximumFractionDigits(2);
        numberFormat.setGroupingUsed(false);
        double result = Double.longBitsToDouble(1);
        try {
            switch (sizeType) {
                case SIZE_TYPE_B:
                    result = Double.valueOf(numberFormat.format(fileS)).doubleValue();
                    break;
                case SIZE_TYPE_KB:
                    result = Double.valueOf(numberFormat.format(fileS / 1024.0d)).doubleValue();
                    break;
                case SIZE_TYPE_MB:
                    result = Double.valueOf(numberFormat.format(fileS / 1048576.0d)).doubleValue();
                    break;
                case SIZE_TYPE_GB:
                    result = Double.valueOf(numberFormat.format(fileS / 1.073741824E9d)).doubleValue();
                    break;
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取内部存储可用空间
     * @return
     */
    public static double getInternalStorageAvailableSpace() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        long ret = availableBlocks * blockSize;
        return formatFileSize(ret, SIZE_TYPE_MB);
    }
}
